package utilities;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;

public class CellModif implements PdfPCellEvent {

	private String text = "round";
	private float fill = 1;
	private int color = 0;

	public CellModif(String txt, float fill, int color) {
		this.text = txt;
		this.fill = fill;
		this.color = color;
	}

	public CellModif(String txt, int color) {
		this.text = txt;
		this.color = color;
		this.fill = 0;
	}

	public void cellLayout(PdfPCell cell, Rectangle position,
			PdfContentByte[] canvases) {

		if ("roundRect".equals(text)) {
			roundRect(cell, position, canvases, fill);
		} else if ("rect".equals(text)) {
			rect(cell, position, canvases, fill);
		} else if ("half".equals(text)) {
			half(cell, position, canvases, fill);
		} else if ("line".equals(text)) {
			line(cell, position, canvases, fill, color);
		}

	}

	public void roundRect(PdfPCell cell, Rectangle position,
			PdfContentByte[] canvases, float fill) {
		PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
		cb.saveState();
		cb.setLineWidth(0.01f); // Make a bit thicker than 1.0 default
		cb.roundRectangle(position.getLeft() + 1.5f,
				position.getBottom() + 1.5f, position.getWidth() - 3,
				position.getHeight() - 3, 4);

		switch (color) {
		case 0:
			cb.setCMYKColorStrokeF(0xFf, 0x00, 0xFF, 0x00);
			cb.stroke();
			break;
		case 1:
			cb.setCMYKColorFill(0x00, 0xFF, 0xFF, 0x00);
			cb.fill();
			break;
		case 2:
			cb.setCMYKColorFill(0xFF, 0x00, 0xFF, 0x00);
			cb.fill();
			break;
		case 3:
			cb.setCMYKColorFill(0xff, 0xff, 0x00, 0x00);
			cb.fill();
			break;
		case 4:
			cb.setCMYKColorFill(0x00, 0x00, 0x00, 0x00);
			// cb.setCMYKColorFill(0xff, 0xff, 0xff, 0x00);
			cb.fill();
			break;
		default:
			break;
		}
		cb.restoreState();
	}

	public void rect(PdfPCell cell, Rectangle position,
			PdfContentByte[] canvases, float fill) {
		PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
		cb.saveState();
		cb.setLineWidth(0.01f); // Make a bit thicker than 1.0 default
		cb.rectangle(position.getLeft(), position.getBottom(),
				position.getWidth(), position.getHeight());

		switch (color) {
		case 0:
			cb.setCMYKColorStrokeF(0xFf, 0x00, 0xFF, 0x00);
			cb.stroke();
			break;
		case 1:
			cb.setCMYKColorFill(0x00, 0xFF, 0xFF, 0x00);
			cb.fill();
			break;
		case 2:
			cb.setCMYKColorFill(0xFF, 0x00, 0xFF, 0x00);
			cb.fill();
			break;
		case 3:
			cb.setCMYKColorFill(0xff, 0xff, 0x00, 0x00);
			cb.fill();
			break;
		case 4:
			cb.setCMYKColorFill(0x00, 0x00, 0x00, 0x00);

			cb.fill();
			break;
		default:
			break;
		}
		cb.restoreState();
	}

	public void half(PdfPCell cell, Rectangle position,
			PdfContentByte[] canvases, float fill) {
		PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
		cb.saveState();
		cb.setLineWidth(0.01f); // Make a bit thicker than 1.0 default
		cb.roundRectangle(position.getLeft() + 1.5f,
				position.getBottom() + 1.5f, position.getWidth() - 3,
				position.getHeight() - 3, 4);

		switch (color) {
		case 0:
			cb.setCMYKColorStrokeF(0xFf, 0x00, 0xFF, 0x00);
			cb.stroke();
			break;
		case 1:
			cb.setCMYKColorFill(0x00, 0xFF, 0xFF, 0x00);
			cb.fill();
			break;
		case 2:
			cb.setCMYKColorFill(0xFF, 0x00, 0xFF, 0x00);
			cb.fill();
			break;
		case 3:
			cb.setCMYKColorFill(0xff, 0xff, 0x00, 0x00);
			cb.fill();
			break;
		case 4:
			cb.setCMYKColorFill(0x00, 0x00, 0x00, 0x00);

			cb.fill();
			break;
		default:
			break;
		}
		cb.restoreState();
	}

	public void line(PdfPCell cell, Rectangle position,
			PdfContentByte[] canvases, float fill, int color) {
		PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
		cb.saveState();

		cb.setLineWidth(0.01f); // Make a bit thicker than 1.0 default
		if (color == 0)
			cb.setColorStroke(BaseColor.WHITE);
		cb.moveTo(position.getLeft() + fill + 4, position.getBottom() + 4);
		cb.lineTo(position.getLeft() + position.getWidth(),
				position.getBottom() + 4);

		cb.stroke();
		cb.restoreState();
	}

}
